Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Python Tuples
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Introduction to Tuple in Python

Tuples in Python are immutable sequences, used to store multiple items in a single variable. They are similar to lists but are immutable, meaning their contents cannot be changed after they are created. This immutability makes tuples a reliable data structure for storing data that must not be modified, and they are often used for data integrity and threading where objects need to be unchanged.

Image

Defining a Tuple

Tuples are defined by placing a sequence of values separated by commas, with or without the use of parentheses. This is known as tuple packing.

Example

In this example, we will define a tuple using parentheses and without them.

Python3

. . . .

Explanation:

  • my_tuple uses parentheses to define the tuple.
  • another_tuple demonstrates tuple packing without parentheses, automatically creating a tuple from the provided items.

Accessing a Tuple Element

Elements in a tuple can be accessed via their index, similar to lists. The indexing starts at 0.

Example

In this example, we will access elements from a tuple using positive indexing.

Python3

. . . .

Explanation:

  • my_tuple[0] accesses the first element of my_tuple, which is 1.
  • my_tuple[2] accesses the third element of my_tuple, which is 3.

Updating a Tuple Element

Since tuples are immutable, their elements cannot be changed directly. However, you can simulate updating by converting the tuple to a list, changing the list, and converting it back to a tuple.

Example

In this example, we will simulate updating an element within a tuple by using list conversion.

Python3

. . . .

Explanation:

  • The tuple is converted to a list to allow for item assignment.
  • The second element is changed to 20.
  • The list is converted back to a tuple, reflecting the update.

Loop Through the Tuple

Iterating over a tuple uses a for-loop to access each item individually.

Example

In this example, we will loop through a tuple to print each item.

Python3

. . . .

Explanation:

  • The for loop iterates over each element in my_tuple, printing them on the same line separated by spaces.

Tuple Operators

Operators in Python can be used with tuples to perform concatenation, repetition, and membership tests. These operations are similar to those used with lists but are applied to tuples, which are immutable.

Tuple Operators

Here is a summary of operators that can be used with tuples:

OperatorDescriptionExample Usage
+Concatenates two tuples into a new tuple.tuple1 + tuple2
*Repeats the contents of a tuple a given number of times.tuple1 * 3
inChecks if an element exists within the tuple.element in tuple1
not inChecks if an element does not exist within the tuple.element not in tuple1

Example Demonstrating Tuple Operators

In this example, we will use tuple operators to concatenate two tuples, repeat a tuple, and perform membership tests.

Python3

. . . .

Explanation:

  • Concatenation: Combines tuple1 and tuple2 into concatenated_tuple.
  • Repetition: Creates repeated_tuple by repeating tuple1 three times.
  • Membership tests: Checks for the presence of 3 in tuple1 and the absence of 7.

Slicing the Tuple

Slicing allows you to create a new tuple by extracting elements from an existing tuple based on their indices.

How Slicing Works

  • Positive Indexing: Starts from the beginning of the tuple with index 0 and goes up to n-1 for n elements.
  • Negative Indexing: Starts from the end of the tuple with index -1 for the last element, -2 for the second last, and so forth.

Example

In this example, we will slice a tuple using both positive and negative indexes.

Python3

. . . .

Explanation:

  • Positive Slice: Extracts elements from index 2 to 6.
  • Negative Slice: Achieves the same result using negative indexing, starting from the end.

This lesson has covered essential aspects of tuples in Python, from basic operations and slicing to the nuances of tuple operators and the flexibility of tuple syntax. Understanding these concepts provides a solid foundation for using tuples effectively in Python programming.

Quiz
Python - Unpack Tuple Items
Mark as Completed

On this page

Defining a Tuple

Example

Accessing a Tuple Element

Example

Updating a Tuple Element

Example

Loop Through the Tuple

Example

Tuple Operators

Tuple Operators

Example Demonstrating Tuple Operators

Slicing the Tuple

Example